package cn.tedu.loding.service.impl;

import cn.tedu.loding.ex.ServiceException;
import cn.tedu.loding.mapper.*;
import cn.tedu.loding.pojo.entity.BrandCategory;
import cn.tedu.loding.pojo.entity.CategoryAttributeTemplate;
import cn.tedu.loding.service.IBrandCategoryService;
import cn.tedu.loding.web.ServiceCode;
import cn.tedu.loding.service.ICategoryAttributeTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 处理类别与属性模板关联数据业务的实现类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Service
public class CategoryAttributeTemplateServiceImpl implements ICategoryAttributeTemplateService {

    @Autowired
    private CategoryAttributeTemplateMapper categoryAttributeTemplateMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AttributeTemplateMapper attributeTemplateMapper;

    public CategoryAttributeTemplateServiceImpl() {
        log.info("创建业务对象：CategoryAttributeTemplateServiceImpl");
    }

    @Override
    public void bind(Long categoryId, Long attributeTemplateId) {
        log.debug("开始处理【绑定类别与属性模板的关联】的业务，类别：{}，属性模板：{}", categoryId, attributeTemplateId);

        // 检查类别是否存在
        {
            Object queryResult = categoryMapper.getStandardById(categoryId);
            if (queryResult == null) {
                String message = "绑定类别与属性模板的关联失败！类别不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }

        // 检查属性模板是否存在
        {
            Object queryResult = attributeTemplateMapper.getStandardById(attributeTemplateId);
            if (queryResult == null) {
                String message = "绑定类别与属性模板的关联失败！属性模板不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }

        // 检查关联是否存在
        {
            int count = categoryAttributeTemplateMapper.countByCategoryIdAndAttributeTemplateId(categoryId, attributeTemplateId);
            if (count > 0) {
                String message = "绑定类别与属性模板的关联失败！此类别与属性模板的绑定已存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        // 创建对象
        CategoryAttributeTemplate categoryAttributeTemplate = new CategoryAttributeTemplate();
        // 复制类别与属性模板的绑定
        categoryAttributeTemplate.setCategoryId(categoryId);
        categoryAttributeTemplate.setAttributeTemplateId(attributeTemplateId);
        // 执行插入数据
        int rows = categoryAttributeTemplateMapper.insert(categoryAttributeTemplate);
        if (rows != 1) {
            String message = "绑定类别与属性模板的关联失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void unbind(Long id) {
        log.debug("开始处理【解绑类别与属性模板的关联】的业务，参数：{}", id);
        // 检查尝试删除的类别与属性模板的绑定是否存在
        Object queryResult = categoryAttributeTemplateMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "解绑类别与属性模板的关联失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 执行删除
        log.debug("即使删除id为{}的类别与属性模板的绑定……", id);
        int rows = categoryAttributeTemplateMapper.deleteById(id);
        if (rows != 1) {
            String message = "解绑类别与属性模板的关联失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.debug("解绑类别与属性模板的关联完成！");
    }

}
